

//
//  NMyQiyeViewController.m
//  nncloud
//
//  Created by macfai on 2018/11/23.
//  Copyright © 2018 macfai. All rights reserved.
//

#import "NMyQiyeViewController.h"
#import "NZhaopinCell.h"
#import "NMiaoshuCell.h"
#import "NMyQiyeModel.h"
@interface NMyQiyeViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataArray;

@property(nonatomic,strong)NSArray *titleArray;
@property(nonatomic,strong)NSArray *infoArray;

@property(nonatomic,strong)NSArray *zhizhaoArray;
@property(nonatomic,strong)NSArray *zhizhaoInfoArray;

@property (nonatomic,copy)NSString *remark;
@property (nonatomic,copy)NSString *picUrl;


@end

@implementation NMyQiyeViewController
#define cellIdentifier @"zhaopin"
#define miaoshuCell @"miaoshu"
#define SectionHeaderHeight  10.f

-(UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - 64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        //        _tableView.rowHeight = 115.f;
        _tableView.separatorStyle = 0;
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"NZhaopinCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        [_tableView registerNib:[UINib nibWithNibName:@"NMiaoshuCell" bundle:nil] forCellReuseIdentifier:miaoshuCell];
        _tableView.showsVerticalScrollIndicator = NO;
        
    }
    return _tableView;
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.view.backgroundColor = kWhiteColor;
    //1企业
    if ([_userType isEqualToString:@"1"]) {
        
        [self.navigationItem setItemWithTitle:@"我的企业" textColor:kNavigationTitleColor fontSize:18 itemType:center];
        
    }else{
        
        [self.navigationItem setItemWithTitle:@"企业详情" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    }
    
    self.titleArray = @[@"审核状态",@"企业名称",@"企业简称",@"所属区域",@"工商注册类型",@"所属行业",@"单位性质",@"统一社会信用代码",@"组织机构代码",];
    //    self.infoArray = @[@"已审核",@"哇哈哈",@"哇哈哈",@"海淀区",@"有限责任公司",@"制糖业",@"国有企业",@"99999999999",@"8765434321",];
    self.zhizhaoArray = @[@"营业执照上传",@"",@"法人代表",@"注册时间",@"注册资金",@"开户银行",@"开户帐号",@"单位电话",@"单位传真",@"企业邮箱",@"企业网站",@"邮政编号",@"单位地址",@"主要产品",@"经营范围",@"主营业务",];
    //    self.zhizhaoInfoArray = @[@"",@"",@"王接口",@"1990-9-8",@"999999",@"招商银行",@"160999999999888",@"18999999999",@"99999999",@"youedata@188.com",@"www.jkjkj.com",@"100000",@"南四环东路",@"大数据分析",@"生产制造",@"大数据增删改查",];
    [self.view addSubview:self.tableView];
    [self loadData];
    // Do any additional setup after loading the view from its nib.
}



- (void)loadData{
    NSDictionary *dic;
    NSString *url;
    if ([_userType isEqualToString:@"1"]) {
        //这个传的是个人用户id
        dic = @{@"enterpriseUserId":[NUserModel shareInstance].userInfoId?[NUserModel shareInstance].userInfoId:@""};
        url = kMyQieyInfoUrl;
    }else{
        //这个需要传的是企业id
        dic = @{@"enterpriseId":_eid?_eid:@""};
        url = kQieyInfoDetailUrl;
    }
    [[NNRCNetRequest manager]getWithUrl:url params:dic success:^(id result) {
        
        YYLog(@"%@",result);
        //        WeakStament(ws);
       
        if ([[result objectForKey:@"code"] integerValue] == 0) {
            //后台返回数据格式混乱做了一个限制
            //        {
            //            msg = "success",
            //            page = "",这个应该返回的是字典才对
            //            code = "0",
            //        }
            if (![result[@"page"] isKindOfClass:[NSDictionary class]]) {
                [AFToast showText:@"参数异常"];
                return ;
            }
            NSDictionary *dict = result[@"page"];
            NMyQiyeModel *model = [NMyQiyeModel mj_objectWithKeyValues:[dict deleteNull]];
            [self.dataArray addObject:model];
            [self formDataWithModel:model];
            [self.tableView reloadData];
        }else{
            [AFToast showText:result[@"msg"]];
        }
    } failure:^(id failure) {
        YYLog(@"%@",failure);
        
    }];
}


- (void)formDataWithModel:(NMyQiyeModel *)model {
    
    //
    
    ///上部信息
    NSString *zhuangtai = model.enterpriseStatus;
    NSString *qiyeName = model.enterpriseName;
    NSString *jiancheng = model.enterpriseSummary;//
    NSString *area = model.enterpriseRegion;
    NSString *zheceType = model.enterpriseBusinessType;
    NSString *hangye = model.enterpriseIndustry;
    NSString *danweiXingzhi = model.enterpriseType;
    NSString *unicomAuthCode = model.enterpriseCreditCode;
    NSString *orgCode = model.enterpriseOrganizationCode;
    
    
    self.infoArray = @[zhuangtai,qiyeName,jiancheng,area,zheceType,hangye,danweiXingzhi,unicomAuthCode,orgCode,];
    ///下部信息
    NSString *faren = model.enterpriseLegalRepresentative;//民营还是国企
    NSString *zhuceTime =  [NSString stringWithFormat:@"%@",model.enterpriseRegisteredTime];
    NSString *money = model.enterpriseRegisteredMoney;
    NSString *bank  = model.enterpriseBankAccount;
    NSString *accout = model.enterpriseAccountNumber;
    NSString *tel = model.enterprisePhone;
    NSString *fax = model.enterpriseFax;
    NSString *email  = model.enterpriseEmail;
    NSString *website = model.enterpriseWebsite;
    NSString *postCode = model.enterprisePostCode;
    NSString *add = model.enterpriseAddress;
    NSString *product  = model.enterpriseProduct;
    NSString *scope = model.enterpriseBusinessScope;
    NSString *business = model.enterpriseMainBusiness;
    NSString *remark = model.enterpriseRemarks;
    self.remark = remark;
    
    
    self.zhizhaoInfoArray = @[@"",@"",faren,zhuceTime,money,bank,accout,tel,fax,email,website,postCode,add,product,scope,business,];
    
    //图片地址
    self.picUrl = model.attachmentUrl;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        
        return 45.f;
        
    }else if (indexPath.section==1){
        if (indexPath.row==1) {
            return 100.f;
        }else{
            return 45.f;
        }
    }
    else if (indexPath.section==2){
        
        return 125.f;
    }
    return 0.f;
    
}
#pragma mark - datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return self.titleArray.count;
    }
    else if (section==1){
        return self.zhizhaoArray.count;
    }
    else if (section==2){
        return 1;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    if (indexPath.section==0) {
        
        NZhaopinCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
        cell.area.hidden = YES;
        cell.date.hidden = YES;
        cell.company.hidden = YES;
        cell.salary.hidden = false;
        cell.name.text = self.titleArray[indexPath.row];
        cell.salary.text = self.infoArray[indexPath.row];
        cell.nameLeftMargin.constant = 25.f;
        return cell;
        
    }else if(indexPath.section==1){
        
        NZhaopinCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
        cell.nameLeftMargin.constant = 25.f;
        cell.area.hidden = YES;
        cell.date.hidden = YES;
        cell.company.hidden = YES;
        cell.salary.hidden = false;
        cell.name.text = self.zhizhaoArray[indexPath.row];
        cell.salary.text = self.zhizhaoInfoArray[indexPath.row];
        
        if (indexPath.row==1) {
            //            cell.backgroundColor = [UIColor magentaColor];
            //添加横线和图片
            UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 3, ScreenWidth, 0.5)];
            line.backgroundColor = kLightGrayColor;
            [cell addSubview:line];
            UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(30,10, 80, 80)];
            NSURL *url = [NSURL URLWithString:self.picUrl?self.picUrl:@""];
            [img sd_setImageWithURL:url placeholderImage:WDImgName(@"网站")];
            [cell addSubview:img];
            
            UIView *sline = [[UIView alloc]initWithFrame:CGRectMake(0, 98, ScreenWidth, 0.5)];
            sline.backgroundColor = kLightGrayColor;
            [cell addSubview:sline];
        }
        
        
        return cell;
        
    }else if (indexPath.section==2){
        
        NMiaoshuCell *cell = [tableView dequeueReusableCellWithIdentifier:miaoshuCell forIndexPath:indexPath];
        cell.nameLabel.text = @"备注";
        cell.detailLable.text = self.remark;
        return cell;
    }
    
    return nil;
}

//根据字符串内容返回字符串的size 工业企业
- (CGSize)sizeWithLabelWithContent:(NSString *)content width:(CGFloat)width font:(UIFont *)font{
    NSDictionary *dict=@{NSFontAttributeName : font};
    CGRect rect=[content boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dict context:nil];
    CGFloat sizeWidth=ceilf(CGRectGetWidth(rect));
    CGFloat sizeHieght=ceilf(CGRectGetHeight(rect));
    return CGSizeMake(sizeWidth, sizeHieght);
}

#pragma mark - section head
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SectionHeaderHeight;//如果设置section的header高度，不设置footer高度，footer默认等于header高度
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, SectionHeaderHeight)];
    view.backgroundColor = kBackgroundColor;
    return view;
    
}

#pragma mark - section foot
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return nil;
}

@end
